import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/index_page.dart';
import 'package:full_display/page/login/email_login/email_login.dart';
import 'package:full_display/page/login/find_account/find_account_widget.dart';
import 'package:full_display/page/login/login_provider.dart';
import 'package:full_display/page/login/phone_login/phone_login.dart';
import 'package:full_display/page/login/register/register_widget.dart';
import 'package:full_display/routers/route_name.dart';
import 'package:full_display/unit/navigation/navigation_util.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/toast/toast_util.dart';
import 'package:full_display/unit/widgets/single_select_widget/single_select_widget.dart';
import 'package:provider/provider.dart';

class LoginWidget extends StatelessWidget {
  const LoginWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => LoginProvider(),
        )
      ],
      child: const LoginPage(),
    );
  }
}

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage>
    with SingleTickerProviderStateMixin {
  var _loginProvider = LoginProvider();

  final _pageController = PageController(initialPage: 0);

  var current = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _loginProvider = context.watch<LoginProvider>();
    return Scaffold(
      backgroundColor: ColorPlate.background,
      resizeToAvoidBottomInset: false,
      body: Container(
        margin: EdgeInsets.only(left: 30.0.W, right: 30.0.W, top: 124.0.H),
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(bottom: 60.0.H),
                alignment: Alignment.centerLeft,
                width: MediaQuery.of(context).size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '你好，',
                      style: StandardTextStyle.loginTopStyle,
                    ),
                    Text(
                      '欢迎使用...！',
                      style: StandardTextStyle.loginTopStyle,
                    )
                  ],
                ),
              ),
              SingleSelectWidget(
                titleList: LoginProvider.titleList,
                callBack: (index) {
                  setState(() {
                    current = index;
                    _pageController.jumpToPage(index);
                  });
                },
              ),
              Container(
                  margin: EdgeInsets.only(top: 31.0.H),
                  height: 134.0.H,
                  child: PageView(
                    onPageChanged: (index) {
                      current = index;
                    },
                    controller: _pageController,
                    children: [
                      PhoneLogin(
                        forgetPassword: () {
                          NavigationUtil.getInstance()
                              .pushPage(context, RouteNames.findAccountWidget,
                                  widget: const FindAccountWidget(
                                    initIndex: 0,
                                  ));
                        },
                        loginCallBack: (map) {
                          _loginProvider.updatePhoneMap(map);
                        },
                      ),
                      EmailLogin(
                        emailCallBack: () {
                          NavigationUtil.getInstance()
                              .pushPage(context, RouteNames.findAccountWidget,
                                  widget: const FindAccountWidget(
                                    initIndex: 1,
                                  ));
                        },
                        emailLoginCallBack: (map) {
                          _loginProvider.updateEmailMap(map);
                        },
                      )
                    ],
                  )),
              GestureDetector(
                onTap: () {
                  _loginProvider.loginByPhoneOrEmail(current, (result) {
                    NavigationUtil.getInstance().pushReplacementPage(
                        context, RouteNames.indexPage,
                        widget: const IndexPage());
                  });
                  // _loginProvider.goLogin('13128901052', '123456');
                },
                child: Container(
                  alignment: Alignment.center,
                  height: 54.0.H,
                  margin: EdgeInsets.only(top: 39.0.H),
                  width: MediaQuery.of(context).size.width,
                  decoration: BoxDecoration(
                      gradient: const LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            ColorPlate.tabbar_select,
                            ColorPlate.tabbar_gradient
                          ]),
                      borderRadius: BorderRadius.circular(27)),
                  child: Text(
                    '登录',
                    style: TextStyle(
                      color: ColorPlate.white,
                      fontSize: 17.0.SP,
                    ),
                  ),
                ),
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  NavigationUtil.getInstance().pushPage(
                      context, RouteNames.registerAccountWidget,
                      widget: const RegisterWidget());
                },
                child: Container(
                  alignment: Alignment.center,
                  height: 54.0.H,
                  margin: EdgeInsets.only(top: 17.0.H),
                  width: MediaQuery.of(context).size.width,
                  child: Text(
                    '立即注册',
                    style: TextStyle(
                      color: ColorPlate.white,
                      fontSize: 14.0.SP,
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
